package top.lixb.libcommon.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.vondear.rxtool.RxImageTool;

import top.lixb.libcommon.R;

public class SimpleItemView extends RelativeLayout {

    private int mDefTextColor = Color.rgb(0x33,0x33,0x33);

    public void setRightIcon(Drawable rightIcon) {
        mRightIcon = rightIcon;
        if (null != mRightIcon) {
            ViewGroup.LayoutParams layoutParams = mIvRight.getLayoutParams();
            layoutParams.height = mRightIconSize;
            layoutParams.width = mRightIconSize;
            mIvRight.setLayoutParams(layoutParams);
            mIvRight.setImageDrawable(mRightIcon);
        }
    }

    private Drawable mRightIcon;
    private int mRightIconSize;

    public Drawable getLeftIcon() {
        return mLeftIcon;
    }

    public void setLeftIcon(Drawable leftIcon) {
        mLeftIcon = leftIcon;
        if (null!=mLeftIcon) {
            ViewGroup.LayoutParams layoutParams = mIvLeft.getLayoutParams();
            layoutParams.height = mLeftIconSize;
            layoutParams.width = mLeftIconSize;
            mIvLeft.setLayoutParams(layoutParams);
            mIvLeft.setImageDrawable(mLeftIcon);
        }
    }

    private Drawable mLeftIcon;
    private int mRightBottomTextColor;
    private int mRightTopTextColor;
    private int mLeftBottomTextColor;
    private int mLeftTopTextColor;
    private float mRightBottomTextSize;
    private float mRightTopTextSize;
    private float mLeftBottomTextSize;
    private float mLeftTopTextSize;
    private String mRightBottomText;
    private int mLeftIconSize;

    public String getRightBottomText() {
        return mRightBottomText;
    }

    public void setRightBottomText(String rightBottomText) {
        mRightBottomText = rightBottomText;
        if (!TextUtils.isEmpty(rightBottomText)) {
            mTvRightBottom.setVisibility(VISIBLE);
            mTvRightBottom.setText(rightBottomText);
        }
    }

    public String getRightTopText() {
        return mRightTopText;
    }

    public void setRightTopText(String rightTopText) {
        mRightTopText = rightTopText;
        if (!TextUtils.isEmpty(rightTopText)) {
            mTvRightTop.setVisibility(VISIBLE);
            mTvRightTop.setText(rightTopText);
        }
    }

    public String getLeftBottomText() {
        return mLeftBottomText;
    }

    public void setLeftBottomText(String leftBottomText) {
        mLeftBottomText = leftBottomText;
        if (!TextUtils.isEmpty(leftBottomText)) {
            mTvleftBottom.setVisibility(VISIBLE);
            mTvleftBottom.setText(leftBottomText);
        }
    }

    public String getLeftTopText() {
        return mLeftTopText;
    }

    public void setLeftTopText(String leftTopText) {
        mLeftTopText = leftTopText;
        if (!TextUtils.isEmpty(leftTopText)) {
            mTvleftTop.setVisibility(VISIBLE);
            mTvleftTop.setText(leftTopText);
        }
    }

    private String mRightTopText;
    private String mLeftBottomText;
    private String mLeftTopText;
    public ImageView mIvLeft;
    public TextView mTvleftTop;
    public TextView mTvleftBottom;
    public TextView mTvRightTop;
    public TextView mTvRightBottom;
    public ImageView mIvRight;
    private boolean mLeftVisiable;
    private boolean mRightVisiable;
    private LinearLayout mLlLeft;
    private LinearLayout mLlRight;

    public SimpleItemView(@NonNull Context context) {
        super(context);
        init(context);
    }

    public SimpleItemView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context,attrs);
        init(context);
    }

    public SimpleItemView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context,attrs);
        init(context);
    }


    private void init(Context context) {
        View view = View.inflate(context, R.layout.layout_common_item, this);
        mIvLeft = view.findViewById(R.id.iv_left);
        mTvleftTop = view.findViewById(R.id.tv_left_top);
        mTvleftBottom = view.findViewById(R.id.tv_left_bottom);
        mTvRightTop = view.findViewById(R.id.tv_right_top);
        mTvRightBottom = view.findViewById(R.id.tv_right_bottom);
        mIvRight = view.findViewById(R.id.iv_right);
        mLlLeft = view.findViewById(R.id.llLeft);
        mLlRight = view.findViewById(R.id.llRight);

        if (TextUtils.isEmpty(mLeftTopText)) {
            mTvleftTop.setVisibility(GONE);
        }else {
            mTvleftTop.setText(mLeftTopText);
            mTvleftTop.setTextColor(mLeftTopTextColor);
            mTvleftTop.setTextSize(TypedValue.COMPLEX_UNIT_PX,mLeftTopTextSize);
        }

        if (TextUtils.isEmpty(mLeftBottomText)) {
            mTvleftBottom.setVisibility(GONE);
        }else {
            mTvleftBottom.setText(mLeftBottomText);
            mTvleftBottom.setTextColor(mLeftBottomTextColor);
            mTvleftBottom.setTextSize(TypedValue.COMPLEX_UNIT_PX,mLeftBottomTextSize);
        }

        if (TextUtils.isEmpty(mRightTopText)) {
            mTvRightTop.setVisibility(GONE);
        }else {
            mTvRightTop.setText(mRightTopText);
            mTvRightTop.setTextColor(mRightTopTextColor);
            mTvRightTop.setTextSize(TypedValue.COMPLEX_UNIT_PX,mRightTopTextSize);
        }

        if (TextUtils.isEmpty(mRightBottomText)) {
            mTvRightBottom.setVisibility(GONE);
        }else {
            mTvRightBottom.setText(mRightBottomText);
            mTvRightBottom.setTextColor(mRightBottomTextColor);
            mTvRightBottom.setTextSize(TypedValue.COMPLEX_UNIT_PX,mRightBottomTextSize);
        }

        if (!mLeftVisiable) {
            mIvLeft.setVisibility(GONE);
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mLlLeft.getLayoutParams();
            layoutParams.leftMargin = 0;
            mLlLeft.setLayoutParams(layoutParams);
        } else {
            if (null!=mLeftIcon) {
                ViewGroup.LayoutParams layoutParams = mIvLeft.getLayoutParams();
                layoutParams.height = mLeftIconSize;
                layoutParams.width = mLeftIconSize;
                mIvLeft.setLayoutParams(layoutParams);
                mIvLeft.setImageDrawable(mLeftIcon);
            }
        }

        if (!mRightVisiable) {
            mIvRight.setVisibility(GONE);
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mLlRight.getLayoutParams();
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            layoutParams.rightMargin = 0;
            mLlRight.setLayoutParams(layoutParams);
        } else {
            if (null != mRightIcon) {
                ViewGroup.LayoutParams layoutParams = mIvRight.getLayoutParams();
                layoutParams.height = mRightIconSize;
                layoutParams.width = mRightIconSize;
                mIvRight.setLayoutParams(layoutParams);
                mIvRight.setImageDrawable(mRightIcon);
            }
        }
    }

    /**
     * dip 转 px
     */
    private int dip2px(int dip) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dip, getResources().getDisplayMetrics());
    }

    private void initAttrs(Context context,AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.common_item);

        mLeftTopText = array.getString(R.styleable.common_item_leftTopText);
        mLeftBottomText = array.getString(R.styleable.common_item_leftBottomText);
        mRightTopText = array.getString(R.styleable.common_item_rightTopText);
        mRightBottomText = array.getString(R.styleable.common_item_rightBottomText);


        mLeftTopTextSize =  array.getDimensionPixelSize(R.styleable.common_item_leftTopTextSize, dip2px(14));
        mLeftBottomTextSize = array.getDimensionPixelSize(R.styleable.common_item_leftBottomTextSize,dip2px(14));
        mRightTopTextSize = array.getDimensionPixelSize(R.styleable.common_item_rightTopTextSize,dip2px(14));
        mRightBottomTextSize = array.getDimensionPixelSize(R.styleable.common_item_rightBottomTextSize,dip2px(14));


        mLeftTopTextColor = array.getColor(R.styleable.common_item_leftTopTextColor, mDefTextColor);
        mLeftBottomTextColor = array.getColor(R.styleable.common_item_leftBottomTextColor, mDefTextColor);
        mRightTopTextColor = array.getColor(R.styleable.common_item_rightTopTextColor, mDefTextColor);
        mRightBottomTextColor = array.getColor(R.styleable.common_item_rightBottomTextColor, mDefTextColor);

        mLeftIcon = array.getDrawable(R.styleable.common_item_leftIcon);
        mRightIcon = array.getDrawable(R.styleable.common_item_rightIcon);
        mLeftVisiable = array.getBoolean(R.styleable.common_item_leftIconVisible,true);
        mRightVisiable = array.getBoolean(R.styleable.common_item_rightIconVisible,true);

        mLeftIconSize = array.getDimensionPixelSize(R.styleable.common_item_leftIconSize, dip2px(20));
        mRightIconSize = array.getDimensionPixelSize(R.styleable.common_item_rightIconSize, dip2px(20));


        array.recycle();
    }
}
